//IE用
if (typeof window.console != 'object') {
    window.console = {log:function(){}};
}
function InterView(media_player){
    var self = this;
    if(typeof media_player == 'object'){
        self.media = media_player;
        self.media_player_id = self.media.id || '';
    }else{
        self.media_player_id = media_player;
    }
    window.swfLoadComplete = function(){
        if(typeof media_player == 'string'){
            self.media = document.getElementById(media_player);
            self.selectEquip();
            self.setQuality();
        }
        if(self.init)self.init.call(self, self);
    };
    window.publishSuccess = function(){
        self.success.call(self,'publishSuccess');
        self.publishSuccess = true;
    };

    window.publishFailed = function(){
        self.error.call(self,'publishFailed');
        self.publishSuccess = false;
    };

    window.publishing = function(){
        self.publishing.call(self);
        self.publishSuccess = false;
    };

    window.receiveSuccess = function(){
        self.success.call(self,'receiveSuccess');
        self.receiveSuccess = false;
    };

    window.receiveFailed = function(){
        self.error.call(self,'receiveFailed');
        self.receiveSuccess = false;
    };

    window.receiveing = function(){
        self.receiveing.call(self);
        self.receiveSuccess = false;
    };

    /**
     * 设备调用后触发事件
     */
    window.equipOK = function(){
//        console.log('设备OK');
        if(self.equipOK)self.equipOK();
    };
    /**
     * 用户点击挂断按钮时
     */
    window.activeOff = function(){
        console.log('点击挂断');
        if(self.activeOff)self.activeOff();
        self.receiveSuccess = self.publishSuccess = false;
    };

    /**
     * 接收停止
     */
    window.receiveStop = function(){
        console.log('接收停止');
        self.receiveSuccess = false;
    };
}

InterView.prototype = {
    media_player_id: null,
    media : null,
    URL :'rtmp://120.131.66.222:1935/live/',
    receiveSuccess: false,
    publishSuccess: false,
    init: function(){},
    error: function(){},
    success: function(){},
    receiveing: function(){},
    publishing: function(){},
    activeOff: function(){},
    equipOK: function(){},
    __init: function(path,params,width,heigth){
        params = params || {BGColor:"#000000","allowFullScreen":"true","wmode":"transparent"};
        swfobject.embedSWF(path+"swfJs/maoSui.swf", this.media_player_id, width||'480', heigth||"410", "11",path+"swfJs/expressInstall.swf",null,params,null);
    },
    receive: function(number){
        if(this.media)this.media.receiveVideo(this.URL,number?'maosui_' + number:number);
        console.log('receive','maosui_' + number)
    },
    receiveStop: function(){
        if(this.media)this.media.receiveStop();
    },
    publish: function(number){
        if(this.media)this.media.publishVideo(this.URL,number ? 'maosui_' + number : number);
        console.log('publish','maosui_' + number)
    },
    hangUp: function(){
        if(this.media)this.media.hangUp();
    },
    receiveStop: function(){
        if(this.media)this.media.receiveStop();
        this.receiveSuccess = false;
    },
    selectEquip: function(){
        if(this.media)this.media.selectEquip(false);
    },
    setQuality: function(){
        if(this.media)this.media.setQuality(30);
    },
    showMask: function(html, id){
        if(!this.media || !html)return false;
        if(!id)id = id || 'popup';
        var pop = $('#' + id );
        if(pop.length == 0){
            pop = $('<div id="'+ id +'" class="popup_bg"></div>').appendTo($(document.body));
        }
        var view = $(this.media);
        var _resize = function(){
            pop.css({
                left: view.offset().left,
                top: view.offset().top
            });
        };
        _resize();
        $(window).resize(_resize);
        if(html)pop.html(html);
        return pop;
    },
    comet: {
        connect: function(){

        }
    }
};
